package com.tencent.wxcloudrun.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tencent.wxcloudrun.model.Categories;
import com.tencent.wxcloudrun.service.CategoriesService;
import com.tencent.wxcloudrun.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2023/2/16 19:40
 * @description: TODO
 */
@RestController
public class CategoriesController {
    @Autowired
    private CategoriesService categoriesService;

    /**
     * 订阅管理待完成
     * @param request
     * @param jsonObject
     * @return
     */
    @GetMapping(value = "/categories/postsubscription")
    JSONObject getCategories(HttpServletRequest request, @RequestBody JSONObject jsonObject){
        String openid = request.getHeader("X-WX-OPENID");
        String categoryid = jsonObject.getString("categoryid");
        JSONObject jsonObject1 = new JSONObject();
        /**
         * 200 订阅成功
         * 201 取消订阅
         */
         jsonObject1.put("status",200);
      return jsonObject1;
    }

    @GetMapping(value = "/categories")
    List<Categories>getCategory(){
        List<Categories> categories = categoriesService.getCategory();
        List<Categories> categories2 = new ArrayList<>();
        for (int i = 0; i < categories.size(); i ++) {
            Categories categories1 = categories.get(i);
            categories1.set_links(JSON.parseObject(categories1.get_linksJson()));
            categories1.set_linksJson("");
            categories1.setMeta(JSON.parseArray(categories1.getMetaJson()));
            categories1.setMetaJson("");
            categories2.add(categories1);
        }
        return   categories2;
    }

    @GetMapping(value = "/categories/{id}")
    Categories getCategoryInfo(@PathVariable String id){
        Categories  categories =  categoriesService.getCategoryInfo(id);

        return   categories;
    }
}
